const query = require('../db/index')
const moment = require('moment')
const bcrypt = require('bcryptjs')


class UserController {
    static instance
    static getInstance () {
        if (!this.instance) {
            this.instance = new UserController()
        }
        return this.instance
    }
    async getInfo (req, res, next) {
        try {
            const querySql = 'select * from ev_users where u_id = ?'
            const queryParams = [req.auth.u_id]
            const result = await query(querySql, queryParams)
            if (result.length === 0) return res.err(result)
            delete result.u_password
            console.log(result)
            res.suc('请求', result)
        } catch (error) {
            console.log('error', error)
            res.err(error, 500)
        }
    }
    async updateInfo (req, res, next) {
        try {
            const updateSql = 'update ev_users set u_username = ? ,u_nickname = ?,u_email = ?,u_user_pic = ? where u_id = ?'
            const updateParams = [
                req.body.username,
                req.body.nickname || null,
                req.body.email || null,
                req.body.user_pic || null,
                req.body.id
            ]
            const result = await query(updateSql, updateParams)
            if (result.affectedRows <= 0) return res.err(result.info)
            res.suc('更新用户信息')
        } catch (error) {
            res.err(error, 500)
        }
    }
    async modifyPassword (req, res, next) {
        try {
            const querySql = 'select u_password from ev_users where u_id = ?'
            const queryParams = [
                req.body.id
            ]
            const queryRes = await query(querySql, queryParams)
            if (queryRes && queryRes.length > 0) {
                const compare = bcrypt.compareSync(req.body.password, queryRes[0].u_password)
                if (compare) return res.err('新密码不能跟旧密码相同')
                const updateSql = 'update ev_users set u_password = ?  where  u_id = ?'
                const updateParams = [
                    bcrypt.hashSync(req.body.password, 10),
                    req.body.id
                ]
                const result = await query(updateSql, updateParams)
                if (result.affectedRows <= 0) return res.err(result.info)
                res.suc('更新密码')
            } else {
                return rs.err('更新密码')
            }
            console.log('queryRes', queryRes)

        } catch (error) {
            res.err(error, 500)
        }
    }
    async updateAvater (req, res, next) {
        try {
            const sql = 'update ev_users set u_user_pic = ? where u_id = ?'
            console.log('bvbbbbb', req.body)
            const params = [
                req.body.user_pic || null,
                req.body.id
            ]
            console.log('params', params)
            const result = await query(sql, params)
            if (result && result.affectedRows > 0) {
                res.suc('头像更新')
            } else {
                res.err('头像更新')
            }
        } catch (error) {
            res.err(error, 500)
        }
    }

}
module.exports = UserController.getInstance()